package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 这个是游戏的窗体类
 * @author jk
 * @date 2021/2/21 0:19
 */
public class Frame extends JFrame {

    /**
     * 我们要实现的是一个动态的窗体
     * 根据传入的数组画一个地图
     */
    JFrame jFrame;
    JButton q;
    JButton qiang;
    static JButton objT;
    int count=0;
    int x=0;
    int y=0;
    static int x1=0;
    static int y1=0;
    JButton duixiang[][] = new JButton[10][10];

    public JButton getObjT() {
        return objT;
    }

    public void setObjT(JButton objT) {
        this.objT = objT;
    }

    public JButton getQ() {
        return q;
    }

    public void setQ(JButton q) {
        this.q = q;
    }

    public JButton getQiang() {
        return qiang;
    }

    public void setQiang(JButton qiang) {
        this.qiang = qiang;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }


    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public JButton[][] getDuixiang() {
        return duixiang;
    }

    public void setDuixiang(JButton[][] duixiang) {
        this.duixiang = duixiang;
    }

    public Frame(JButton objT, JButton q, JButton qiang, int count, int x, int y, int x1, int y1, JButton[][] duixiang) throws HeadlessException {
        this.objT = objT;
        this.q = q;
        this.qiang = qiang;
        this.count = count;
        this.x = x;
        this.y = y;
        this.x1 = x1;
        this.y1 = y1;
        this.duixiang = duixiang;
    }

    public Frame() {
    }

    public Frame(int[][] map) {

        this.setSize(1010, 1000);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        map[0][0]=MapElementConstant.OPEN_SPACE;
        map[0][1]=MapElementConstant.OPEN_SPACE;
        map[1][0]=MapElementConstant.OPEN_SPACE;
        map[2][1]=MapElementConstant.OPEN_SPACE;
        map[1][2]=MapElementConstant.OPEN_SPACE;
        map[1][1]=MapElementConstant.OPEN_SPACE;

        JButton q = new JButton();
        this.add(q);
        JButton w = new JButton();
        this.add(w);
        q.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\人物头像.jpg"));
        q.setBounds(x,y,100,100);
        w.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\人物头像女.jpg"));
        w.setBounds(x1,y1,100,100);

        KeyAdapter kl = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                JButton objT = (JButton) e.getSource();
                objT.setFocusable(true);
                if (e.getKeyCode() == KeyEvent.VK_P) {
                    MyThread t = new MyThread(Frame.this, map, duixiang);
                    t.start();
                }
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    if (y <= 0) {
                        y = 0;
                        return;
                    } else if (map[x / 100][(y - 100) / 100] == 0) {
                        return;
                    } else {
                        objT.setBounds(objT.getX(), objT.getY() - 100, 100, 100);
                    }
                    y = y - 100;
                    System.out.println("y=:" + y);
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    if (map[(x) / 100][(y + 100) / 100] == 0) {
                        return;
                    }
                    if (y >= 600) {
                        y = 500;
                    } else {
                        objT.setBounds(objT.getX(), objT.getY() + 100, 100, 100);
                    }
                    y = y + 100;
                    System.out.println("y=:" + y);
                } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if (x <= 0) {
                        x = 0;
                        return;
                    } else if (map[(x - 100) / 100][y / 100] == 0) {
                        return;
                    } else if (x >= 0) {
                        objT.setBounds(objT.getX() - 100, objT.getY(), 100, 100);
                    }
                    x = x - 100;
                    System.out.println("x=:" + x);
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if (map[(x + 100) / 100][y / 100] == 0) {
                        return;
                    }
                    if (x >= 900) {
                        x = 800;
                    } else {
                        objT.setBounds(objT.getX() + 100, objT.getY(), 100, 100);
                    }
                    x = x + 100;
                    System.out.println("x=:" + x);
                }
            }
        };

        KeyAdapter kl1 = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                JButton objT1 = (JButton) e.getSource();
                objT1.setFocusable(true);
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    T2 t1 = new T2(Frame.this, map, duixiang);
                    t1.start();
                }
                if (e.getKeyCode() == KeyEvent.VK_W) {
                    if (y1 <= 0) {
                        y1 = 0;
                        return;
                    } else if (map[x1 / 100][(y1 - 100) / 100] == 0) {
                        return;
                    } else {
                        objT1.setBounds(getX1(), getY1() - 100, 100, 100);
                    }
                    y1 = y1 - 100;
                    System.out.println("y=:" + y1);
                } else if (e.getKeyCode() == KeyEvent.VK_S) {
                    if (map[(x1) / 100][(y1 + 100) / 100] == 0) {
                        return;
                    }
                    if (y1 >= 600) {
                        y1 = 500;
                    } else {
                        objT1.setBounds(getX1(), getY1() + 100, 100, 100);
                    }
                    y1 = y1 + 100;
                    System.out.println("y=:" + y1);
                } else if (e.getKeyCode() == KeyEvent.VK_A) {
                    if (x1 <= 0) {
                        x1 = 0;
                        return;
                    } else if (map[(x1 - 100) / 100][y1 / 100] == 0) {
                        return;
                    } else if (x1 >= 0) {
                        objT1.setBounds(getX1() - 100, getY1(), 100, 100);
                    }
                    x1 = x1 - 100;
                    System.out.println("x=:" + x1);
                } else if (e.getKeyCode() == KeyEvent.VK_D) {
                    if (map[(x1 + 100) / 100][y1 / 100] == 0) {
                        return;
                    }
                    if (x1 >= 900) {
                        x1 = 800;
                    } else {
                        objT1.setBounds(getX1() + 100, getY1(), 100, 100);
                    }
                    x1 = x1 + 100;
                    System.out.println("x=:" + x1);

                }
            }

        };

        q.addKeyListener(kl);w.addKeyListener(kl1);


        for (int i = 0; i <10; i++) {
            for (int j = 0; j <10; j++) {
                count++;
                System.out.print(map[i][j]);
                if (count==10){
                    System.out.println();
                    count=0;
                }
                if (map[i][j] == MapElementConstant.WALL){
                    JButton o = new JButton();
                    duixiang[i][j]=o;
                    o.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\墙.jpg"));
                    o.setBounds(i*100, j*100, 100, 100);
                    this.add(o);
                }else {
                    JButton p = new JButton();
                    duixiang[i][j]=p;
                }
            }
        }
    }
}


class MyThread extends Thread{
    JFrame jFrame;
    int[][] map;
    JButton duixiang[][];
    public MyThread(JFrame jFrame,int[][] map,Object duixiang[][]){
        this.jFrame=jFrame;
        this.map=map;
        this.duixiang= (JButton[][]) duixiang;
    }
    public void run(){
        System.out.println("T1线程执行了");
        JButton a = new JButton();
        jFrame.add(a);
        a.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\1.jpg"));
        a.setBounds(jFrame.getX(), jFrame.getY(), 100, 100);
        int zhongjian= jFrame.getX();
        int zhongjian1= jFrame.getY();

        try {
            MyThread.sleep(1000);
            jFrame.remove(a);

            JButton b = new JButton();
            JButton b1 = new JButton();
            JButton b2 = new JButton();
            JButton b3 = new JButton();
            JButton b4 = new JButton();
            jFrame.add(b);
            jFrame.add(b1);
            jFrame.add(b2);
            jFrame.add(b3);
            jFrame.add(b4);
            b.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b1.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b2.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b3.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b4.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));

            b.setBounds(zhongjian, zhongjian1, 100, 100);
            if (zhongjian1 >= 100) {
                b1.setBounds(zhongjian, zhongjian1 - 100, 100, 100);
            }
            if (zhongjian >= 100) {
                b2.setBounds(zhongjian - 100, zhongjian1, 100, 100);
            }
            if (zhongjian <= 800) {
                b3.setBounds(zhongjian + 100, zhongjian1, 100, 100);
            }
            if (zhongjian1 <= 500) {
                b4.setBounds(zhongjian, zhongjian1 + 100, 100, 100);
            }
            MyThread.sleep(500);

            if (zhongjian1 >= 100) {
                if (map[zhongjian / 100][(zhongjian1 - 100) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[zhongjian / 100][(zhongjian1 - 100) / 100]);
                    map[zhongjian / 100][(zhongjian1 - 100) / 100]=MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian >=100) {
                if (map[(zhongjian - 100) / 100][(zhongjian1) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[(zhongjian - 100) / 100][zhongjian1 / 100]);
                    map[(zhongjian - 100) / 100][(zhongjian1) / 100] = MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian <= 800) {
                if (map[(zhongjian + 100) / 100][(zhongjian1) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[(zhongjian + 100) / 100][zhongjian1 / 100]);
                    map[(zhongjian + 100) / 100][(zhongjian1) / 100] = MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian1<=500){
                if (map[zhongjian/100][(zhongjian1+100)/100]==MapElementConstant.WALL){
                    jFrame.remove(duixiang[zhongjian/100][(zhongjian1+100)/100]);
                    map[zhongjian/100][(zhongjian1+100)/100]=MapElementConstant.OPEN_SPACE;
                }
            }
            jFrame.remove(b);
            jFrame.remove(b1);
            jFrame.remove(b2);
            jFrame.remove(b3);
            jFrame.remove(b4);
            jFrame.repaint();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}


class T2 extends Thread{
    JFrame jFrame;
    int[][] map;
    JButton duixiang[][];
    public T2(JFrame jFrame,int[][] map,Object duixiang[][]){
        this.jFrame=jFrame;
        this.map=map;
        this.duixiang= (JButton[][]) duixiang;
    }
    public void run(){
        System.out.println(Frame.x1);
        System.out.println(Frame.y1);
        JButton a = new JButton();
        jFrame.add(a);
        a.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\1.jpg"));
        a.setBounds(Frame.x1, Frame.y1, 100, 100);
        int zhongjian= Frame.x1;
        int zhongjian1= Frame.y1;


        try {
            MyThread.sleep(1000);
            jFrame.remove(a);

            JButton b = new JButton();
            JButton b1 = new JButton();
            JButton b2 = new JButton();
            JButton b3 = new JButton();
            JButton b4 = new JButton();
            jFrame.add(b);
            jFrame.add(b1);
            jFrame.add(b2);
            jFrame.add(b3);
            jFrame.add(b4);
            b.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b1.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b2.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b3.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));
            b4.setIcon(new ImageIcon("D:\\java\\夏宇阳的程序\\xiayy-programming-learing\\src\\ressource\\火花.png"));

            b.setBounds(zhongjian, zhongjian1, 100, 100);
            if (zhongjian1 >= 100) {
                b1.setBounds(zhongjian, zhongjian1 - 100, 100, 100);
            }
            if (zhongjian >= 100) {
                b2.setBounds(zhongjian - 100, zhongjian1, 100, 100);
            }
            if (zhongjian <= 800) {
                b3.setBounds(zhongjian + 100, zhongjian1, 100, 100);
            }
            if (zhongjian1 <= 500) {
                b4.setBounds(zhongjian, zhongjian1 + 100, 100, 100);
            }
            MyThread.sleep(500);

            if (zhongjian1 >= 100) {
                if (map[zhongjian / 100][(zhongjian1 - 100) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[zhongjian / 100][(zhongjian1 - 100) / 100]);
                    map[zhongjian / 100][(zhongjian1 - 100) / 100]=MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian > +100) {
                if (map[(zhongjian - 100) / 100][(zhongjian1) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[(zhongjian - 100) / 100][zhongjian1 / 100]);
                    map[(zhongjian - 100) / 100][(zhongjian1) / 100] = MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian <= 800) {
                if (map[(zhongjian + 100) / 100][(zhongjian1) / 100] == MapElementConstant.WALL) {
                    jFrame.remove(duixiang[(zhongjian + 100) / 100][zhongjian1 / 100]);
                    map[(zhongjian + 100) / 100][(zhongjian1) / 100] = MapElementConstant.OPEN_SPACE;
                }
            }
            if (zhongjian1<=500){
                if (map[zhongjian/100][(zhongjian1+100)/100]==MapElementConstant.WALL){
                    jFrame.remove(duixiang[zhongjian/100][(zhongjian1+100)/100]);
                    map[zhongjian/100][(zhongjian1+100)/100]=MapElementConstant.OPEN_SPACE;
                }
            }
            jFrame.remove(b);
            jFrame.remove(b1);
            jFrame.remove(b2);
            jFrame.remove(b3);
            jFrame.remove(b4);
            jFrame.repaint();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
